package com.yanqu.road.server.manager.activity.suitang.manager;

import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionJoinData;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangCommanderData;
import com.yanqu.road.entity.enums.eUnionPosition;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSuiTangUnionActivityMgr extends CrossBaseActivityMgr {

    protected static Logger logger = LogManager.getLogger(CrossSuiTangMapActivityMgr.class.getName());

    @Override
    public void init() {
        super.init();
    }

    public void initUnionData() {
        Map<String, CrossSuiTangUnionJoinData> unionJoinDataMap = activity.getUnionJoinDataMap();
        if (unionJoinDataMap == null || unionJoinDataMap.isEmpty()) {
            return;
        }
        Map<String, CrossSuiTangUnionData> unionDataMap = activity.getUnionDataMap();
        if (unionDataMap == null) {
            unionDataMap = new ConcurrentHashMap<>();
        }
        for (CrossSuiTangUnionJoinData unionJoinData : unionJoinDataMap.values()) {
            CrossSuiTangUnionData union = new CrossSuiTangUnionData();
            union.setActivityId(activity.getConfig().getActivityId());
            union.setGroupId(activity.getGroupId());
            union.setUnionUid(unionJoinData.getUnionUid());
            union.setTotalScore(0);

            boolean isInit = !unionDataMap.containsKey(unionJoinData.getUnionUid());
            // 初始化指挥官信息，会长默认为首个指挥官
            CrossSuiTangCommanderData commanderData = new CrossSuiTangCommanderData();
            long masterUserId = unionJoinData.getMasterId();
            commanderData.getUserId().add(masterUserId);
            union.setCommanders(commanderData);
            if (isInit) {
                union.setInsertOption();
            } else if (!union.isNeedInsert()) {
                union.setUpdateOption();
            }
            unionDataMap.put(unionJoinData.getUnionUid(), union);
        }

        // 同步初始化一下商会的轮次数据
        for (CrossSuiTangUnionData unionData : unionDataMap.values()) {
            activity.getRoundActivityMgr().initUnionRoundData(unionData.getUnionUid());
        }

        logger.debug("【隋唐】商会活动数据初始化完成");
    }

    public void resetUnionInfo() {
        for (CrossSuiTangUnionData unionData : activity.getUnionDataMap().values()) {
            // 重置指挥官信息
            unionData.getCommanders().setNotice("");
            unionData.getCommanders().setMajorCityId(0);
            unionData.getCommanders().setMinorCityId(0);
            // 重置 buff 信息
            unionData.getUnionBuffMap().clear();
            unionData.setUpdateOption();
            activity.getSyncActivityMgr().syncCommanderInfoToUnion(unionData.getUnionUid());
        }
    }

    public void onUnionChange(String unionId, List<Long> syncUserList) {
        resetUnionCommander(unionId, syncUserList);
    }

    public void resetUnionCommander(String unionId, List<Long> syncUserList) {
        CrossSuiTangUnionJoinData unionJoin = getUnionJoin(unionId);
        CrossSuiTangUnionData unionData = getUnionData(unionId);
        if (unionData == null || unionJoin == null) {
            return;
        }
        CrossSuiTangCommanderData commanders = unionData.getCommanders();
        if (commanders == null || commanders.getUserId() == null) {
            return;
        }
        for (Long userId : syncUserList) {
            if (unionJoin.getMemberMap() == null
                || unionJoin.getMemberMap().isEmpty()
                || !unionJoin.getMemberMap().containsKey(userId)) {
                continue;
            }
            if (unionJoin.getMemberMap().get(userId).getPosition() == eUnionPosition.Master.getValue()) {
                long masterUserId = userId;
                if (!commanders.getUserId().isEmpty() && masterUserId != commanders.getUserId().get(0)) {
                    commanders.getUserId().set(0, masterUserId);
                }
                // 客户端不愿意改，策划也说允许这里两个是同一个人，所以注释掉这部分代码
//                if (commanders.getUserId().size() >= 2 && masterUserId == commanders.getUserId().get(1)) {
//                    commanders.getUserId().remove(1);
//                }
                break;
            }
        }
    }
}
